﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iTunesLib;
using iTunesAdminLib;
using System.IO;
using System.Drawing.Drawing2D;
using System.Xml;
using Ini;

namespace WindowsFormsApplication1
{
    public partial class main : Form
    {
        delegate void Router(object arg);
        iTunesApp myiTunes = new iTunesAppClass();

        public main()
        {
            myiTunes = new iTunesAppClass();
            myiTunes.OnPlayerPlayEvent += new _IiTunesEvents_OnPlayerPlayEventEventHandler(delegate(object o)
            {
                this.Invoke(new Router(myiTunes_OnPlayerPlayEvent), o);
            });
            InitializeComponent();
        }

        public IniFile ini = new IniFile(Application.StartupPath + "\\config.ini");
        private void Form1_Load(object sender, EventArgs e)
        {
            refresh();
            if (!File.Exists(Application.StartupPath + "\\config.ini"))
            {
                ini.IniWriteValue("CFG", "Login", "");
                ini.IniWriteValue("CFG", "Password", "");
            }
            else
            {
                txtlogin.Text = ini.IniReadValue("CFG", "Login");
                txtpassword.Text = ini.IniReadValue("CFG", "Password");
            }
            if (txtlogin.Text == "")
            { tabControl1.SelectedTab = tabPage2; }
            else { tabControl1.SelectedTab = tabPage1; }
            webBrowser1.Navigate("http://nicow.ath.cx/experiments/NowPlaying/?user=" + txtlogin.Text + "&password=" + txtpassword.Text + "&title=" + lbltrack.Text + "&artist=" + lblartist.Text + "&album=" + lblalbum.Text);
        }
        public void myiTunes_OnPlayerPlayEvent(object iTrack)
        {
            refresh();
        }
        public void refresh()
        {
            
            IITTrack track = myiTunes.CurrentTrack;
            try
            {

                lbl_ver.Text = myiTunes.Version;
                lblver2.Text = "0.1.0";
                lblalbum.Text = track.Album;
                lblartist.Text = track.Artist;
                lbltrack.Text = track.Name;
                }
            catch (NullReferenceException e)
            {
                lbl_ver.Text = "";
                lblalbum.Text = "";
                lblartist.Text = "Aucune lecture en cours.";
                lbltrack.Text = "";
                }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            XmlTextReader reader = new XmlTextReader("http://nicow.ath.cx/experiments/NowPlaying/test.xml");
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element: // The node is an element.
                        Console.Write("<" + reader.Name);
                        Console.WriteLine(">");
                        break;
                    case XmlNodeType.Text: //Display the text in each element.
                        listBox1.Items.Add(reader.Value);
                        break;
                    case XmlNodeType.EndElement: //Display the end of the element.
                        Console.Write("</" + reader.Name);
                        Console.WriteLine(">");
                        break;
                }
            }
            Console.ReadLine();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ini.IniWriteValue("CFG", "Login", txtlogin.Text);
            ini.IniWriteValue("CFG", "Password", txtpassword.Text);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://nicow.ath.cx/experiments/NowPlaying/?user=" + txtlogin.Text + "&password=" + txtpassword.Text + "&title=" + lbltrack.Text + "&artist=" + lblartist.Text + "&album=" + lblalbum.Text);
        }


        


        
    }
}
